/**
 * @Author: PanRS
 * @CreateDate：2014年11月14日
 * @Version 1.0 
 * TODO 
 * 		winUtil 封装弹出框工具
 * 	 基于：artDialog4.1.7 jquery.artDialog.js
 */

var winUtil;

//将winUtil对象注册给当前浏览器窗口最顶层
if (!top.winUtil) {
	winUtil = {};
} else {
	throw new Error("winUtil has already existed!");
}

function _setSelStatus(id , isAdd) {
	if(isAdd){
		$.each($(top.document).find('div.tool-topRbtn') , function(index , btn){
			var channel = $(this).attr('channel');
			if(channel == id){				
				 $(this).addClass('tool-topRbtn-sel');
			}
		});
	} else {
		$.each($(top.document).find('div.tool-topRbtn-sel') , function(index , btn){
			var channel = $(this).attr('channel');
			if(channel == id){				
				 $(this).removeClass('tool-topRbtn-sel');
			}
		});
	}
};

/**
 * 打开新窗口，新窗口位于当前document的最顶层
 * @param options 参数
 * @param customId 页面点击时间id
 */
winUtil.openCustomDialog = function(options , customObj) {
	
	$(customObj).dialogCustom(options);
	
};

/**
 * 打开新页面
 * @param url jspURL / actionURL....
 * @param callback 回调
 */
winUtil.open = function(url, callback) {
	artDialog.open(url, {
		title : null,
		width : "90%",
		height : "90%",
		zIndex : 9999,
		lock : true,
		opacity : 0.5,
		init : function() {
		}
	});
};

winUtil.pop = function(option){
	artDialog.open(option.url, {
		id:option.id,
		title : option.title,
		width : option.width,
		height : option.height,
		top:option.top,
		left:option.left,
		zIndex : 9999,
		lock : false,
		opacity : 0.5,
		init : option.init,
		close:option.close
	});
};

/**
 * 弹出div等窗口
 * 
 * @param title
 *            窗口名称
 * @param contentId
 *            所要显示div等节点的id
 */
winUtil.showDialog = function(option,title, contentinfo, callback) {
	var d = art.dialog({
		title : title,
		drag : true,
		resize : false,
		width : (option.width==undefined)?220:option.width,
		height:(option.height==undefined)?220:option.height,
		content : contentinfo,
		zIndex : 9999,
		lock : true,
		opacity : 0.2,
		fixed : true,
		init : function() {
			if (callback instanceof Function) {
				callback(true);
			}
		}
	});
	return d;
};
/**
 * 弹出div等窗口
 * 
 * @param title
 *            窗口名称
 * @param contentId
 *            所要显示div等节点的id
 */
winUtil.showDialog2 = function(option,title, contentinfo,islock,top,left,id,callback,closeFun) {
	var d = art.dialog({
		title : title,
		drag : true,
		resize : false,
		id : id,
		width : (option.width==undefined)?220:option.width,
		height:(option.height==undefined)?220:option.height,
		content : contentinfo,
		zIndex : 9999,
		lock : islock,
		top : top,
		left : left,
		opacity : 0,
		fixed : true,
		init : function() {
			if (callback instanceof Function) {
				callback(true);
			}
		},
		close:function(){
			if (closeFun instanceof Function) {
				closeFun(true);
			}
		}
	});
	return d;
};
/**
 * 弹出div等窗口--数值模式 邮票图弹出框 需要有一个关闭的返回方法  因不确认加2个fun会不会对其他方法有影响所以重写上一个方法
 * add by wangling 
 * @param title
 *            窗口名称
 * @param contentId
 *            所要显示div等节点的id
 */
winUtil.showDialogforStamp = function(option,title, contentinfo, callback,closeFun) {
	var d = art.dialog({
		title : title,
		drag : true,
		resize : false,
		width : (option.width==undefined)?220:option.width,
		height:(option.height==undefined)?220:option.height,
		content : contentinfo,
		zIndex : 9999,
		lock : true,
		opacity : 0.2,
		fixed : true,
		init : function() {
			if (callback instanceof Function) {
				callback(true);
			}
		},
		close:function(){
			if (closeFun instanceof Function) {
				closeFun(true);
			}
		}
	});
	return d;
};

/**
 * 资料统计窗口
 * @param callback 回调函数
 * @param winUtil.Statics 资料统计窗口对象
 */
winUtil.Statics = null;
winUtil.StaticsOpter = null;
winUtil.openStatics = function(callback){
	if (winUtil.Statics) {
		return;
	}
	var dbc = $("#defaultBusisCode").val();
	var bcs = $("#busisCodeSet").val();
	var sitety = $("input[name='vo.statType']:checked").val();
	sitety = G_APP.isUndefined(sitety) ? '3' : sitety;
//	var dateStr = $("#dateStr").val();
	var dateStr = '2015-04-09 08';
	var timer = new Date().getTime();
	
	var url = G_APP.PAGECONTEXT + '/common/initStatistics.do?t=' + timer 
								+ '&defaultBusisCode=' + dbc 
								+  '&busisCodeSet=' + bcs
								+  '&sitety=' + sitety
								+  '&dateStr=' + dateStr;
									
	winUtil.Statics = artDialog.open(url, {
		title : '资料统计',
		width : 723,
		height : 320,
		zIndex : 9999,
		lock : false,
		resize : false,
		opacity : 0.1,
		init : function() {
			_setSelStatus('statics' , true);
			if (callback instanceof Function) {
				callback(true);
			}
		},
		close : function() {
			winUtil.Statics = null;
			_setSelStatus('statics' , false);
		}
	});
};


/**
 * 站点类型选择窗口
 * @param defaultty 默认站点类型
 * @param callback 回调函数
 * @param winUtil.SiteWin 站点类型窗口对象
 */
winUtil.SiteWin = null;
winUtil.showSiteWin = function(defaultty, callback) {
	if (winUtil.SiteWin) {
		return;
	}
	var contentinfo = G_APP.STATIONTYPEHTML(defaultty);
	winUtil.SiteWin = art.dialog({
		title : "站点类型",
		drag : true,
		resize : false,
		width : 220,
		content : contentinfo,
		left : 250,
		top : 10,
		lock : false,
		init : function() {
			$("input[name='vo.statType']:checked").parent('li').css({
				'color' : '#F4B52D'
			});
			_setSelStatus('site' , true);
			if (callback instanceof Function) {
				callback(true);
			}
		},
		close : function() {
			winUtil.SiteWin = null;
			_setSelStatus('site' , false);
		}
	});
};

/**
 * 军区选择窗口
 * @param defaultty 默认军区
 * @param callback 回调函数
 * @param winUtil.SiteWin 军区窗口对象
 */
winUtil.areaWin = null;
winUtil.showAreaWin = function(defaultty, callback) {
	if (winUtil.areaWin) {
		throw new Error("junquWin has already existed!");
		return;
	}
	var contentinfo = G_APP.AREATYPESHTML(defaultty);
	winUtil.areaWin = art.dialog({
		title : "军区",
		drag : true,
		resize : false,
		width : 220,
		content : contentinfo,
		left : 250,
		top : 10,
		lock : false,
		init : function() {
			_setSelStatus('junqu' , true);
			if (callback instanceof Function) {
				callback(true);
			}
		},
		close : function() {
			winUtil.areaWin = null;
			_setSelStatus('junqu' , false);
		}
	});
};

/**
 * 天气尺度、中尺度选择窗口
 * @param callback 回调函数
 * @param winUtil.ScaleWin 天气尺度、中尺度窗口对象
 */
winUtil.ScaleWin = null;
winUtil.showScaleWin = function(callback) {
	if (winUtil.ScaleWin) {
		throw new Error("ScaleWin has already existed!");
		return;
	}
	var contentinfo = G_APP.SCALEHTML();
	winUtil.ScaleWin = art.dialog({
		title : "中尺度工具箱",
		drag : true,
		resize : false,
		width : 220,
		height : 448,
		content : contentinfo,
		left : 250,
		top : 125,
		lock : false,
		init : function() {
			_setSelStatus('scalein' , true);
			if (callback instanceof Function) {
				callback(true);
			}
		},
		close : function() {
			winUtil.ScaleWin = null;
			_setSelStatus('scalein' , false);
			if(drawSvgGis.svg.unDraw){
				drawSvgGis.svg.unDraw.deactivate();
			};
		}
	});
};

/**
 * 图层窗口
 * @param callback 回调函数
 * @param layPros  要展示的图层
 * @param winUtil.LayWin 天气尺度、中尺度窗口对象
 */
winUtil.LayWin = null;
winUtil.showLayWin = function(callback) {
	if (winUtil.LayWin) {
		throw new Error("ScaleWin has already existed!");
		return;
	}
	winUtil.LayWin = art.dialog({
		title : "图层设置",
		drag : true,
		resize : false,
		width : 330,
		height : 450,
		content : G_APP.LAYER_HTML(),
		lock : false,
		init : function() {
			_setSelStatus('layer' , true);
			
			$('.lay-tt').unbind('click').bind('click' , function(){
				var curStatus = $(this).hasClass('layer-accord-open');
				if(curStatus){
					$(this).removeClass('layer-accord-open').addClass('layer-accord-close');
					$(this).closest('li.titleli').next('li.wrapli').slideToggle(300);
				} else {
					$(this).removeClass('layer-accord-close').addClass('layer-accord-open');
					$(this).closest('li.titleli').next('li.wrapli').slideDown(300);
				}
			 });
			$('.layerli').unbind('click').bind('click', function(){
				var id = $(this).attr("id");
				if(!G_APP.isUndefined(id)) {
					if($(this).hasClass('layeyeopen')) {
						$(this).removeClass("layeyeopen").addClass("layeyeclose");
						layerControl(id,'false');
						return;
					}
					if($(this).hasClass('layeyeclose')) {
						$(this).removeClass("layeyeclose").addClass("layeyeopen");
						layerControl(id,'true');
						return;
					}
				}
				
			});
			if (callback instanceof Function) {
				callback(true);
			}
		},
		close : function() {
			winUtil.LayWin = null;
			_setSelStatus('layer' , false);
		}
	});
};

/**
 * Titan单体信息窗口
 * @param callback 回调函数
 * @param winUtil.TitanMonocase 单体信息窗口
 */
winUtil.TitanMonocase = null;
winUtil.initTitanMonocase = function(url , callback){
	
	if (winUtil.TitanMonocase) {
		return;
	}
	winUtil.TitanMonocase = artDialog.open(url, {
		title : '单体信息',
		width : 602,
		height : 360,
		zIndex : 9999,
		lock : false,
		resize : false,
		opacity : 0.5,
		follow : document.getElementById('titanpup'),
		init : function() {
			if (callback instanceof Function) {
				callback(true);
			}
		},
		close : function() {
			winUtil.TitanMonocase = null;
		}
	});
};

/**
 * 冰雹、风暴识别信息
 * @param callback 回调函数
 * @param winUtil.Recognition 冰雹、风暴识别信息窗口
 */
winUtil.Recognition = null;
winUtil.initRecognition = function( obj , url , reqparams , callback ){
	if (winUtil.Recognition) {
		return;
	}
	$.ajax({
		  url: url,
		  data : reqparams,
		  success: function(resp){
			  var rest =  resp.data;
			  var ul = $('<ul>');
			  ul.append($('<li>').html('站点：' + rest.stationid));
			  ul.append($('<li>').html('观测时间：' + rest.time));
			  ul.append($('<li>').html('中心经度：' + rest.coordX + '°'));
			  ul.append($('<li>').html('中心纬度：' + rest.coordY + '°'));
			  ul.append($('<li>').html('中心方位：' + rest.direction + '°'));
			  ul.append($('<li>').html('中心距离：' + rest.distance + 'km'));
			  ul.append($('<li>').html('冰雹概率：' + rest.prob + '%'));
			  ul.append($('<li>').html('强冰雹概率：' + rest.sprob + '%'));
			  ul.append($('<li>').html('与0度层高度差：' + rest.ilevel + 'km'));
			  ul.append($('<li>').html('预期最大冰雹尺寸：' + rest.expectsize + 'in'));
			  ul.append($('<li>').html('冰雹指数：' + rest.hiexp + 'J/m2/s'));
			  
			  $(obj).tooltip({
					position : 'top',
					content : ul,
					onShow : function(){
						$(this).tooltip('tip').css({
							backgroundColor : 'rgba(255 , 255,255, .9)',
							borderColor : '#304F6E'
						});
					}
			  });
			  
		  }
	});
	
};

/**
 * @returns winArr 所有已经打开的artDialog窗口
 */
winUtil.getOpenedWin = function(){
	var winArr = new Array();
	if(winUtil.Statics) winArr.push(winUtil.Statics);
	if(winUtil.SiteWin) winArr.push(winUtil.SiteWin);
	if(winUtil.areaWin) winArr.push(winUtil.areaWin);
	if(winUtil.ScaleWin) winArr.push(winUtil.ScaleWin);
	if(winUtil.LayWin) winArr.push(winUtil.LayWin);
	if(winUtil.TitanMonocase) winArr.push(winUtil.TitanMonocase);
	return winArr;
};

/**
 * 关闭所有已经打开artDialog窗口
 */
winUtil.closeAllWin = function(){
	if(winUtil.Statics) winUtil.Statics.close();
	if(winUtil.SiteWin) winUtil.SiteWin.close();
	if(winUtil.areaWin) winUtil.areaWin.close();
	if(winUtil.ScaleWin) winUtil.ScaleWin.close();
	if(winUtil.LayWin) winUtil.LayWin.close();
	if(winUtil.TitanMonocase) winUtil.TitanMonocase.close();
};
/**
 * 城镇预报窗口
 * @param callback 回调函数
 * @param winUtil.cityForecast 城镇预报窗口对象
 */
winUtil.Statics = null;
winUtil.StaticsOpter = null;
winUtil.openCityForeacast = function(callback){
	if (winUtil.Statics) {
		return;
	}
	var dbc = $("#defaultBusisCode").val();
	var bcs = $("#busisCodeSet").val();
	var sitety = $("input[name='vo.statType']:checked").val();
	sitety = G_APP.isUndefined(sitety) ? '3' : sitety;
//	var dateStr = $("#dateStr").val();
	var dateStr = '2015-04-09 08';
	var timer = new Date().getTime();
	
	var url = G_APP.PAGECONTEXT + '/subjectAnalysis/initCityForecast.do?t=' + timer 
							//	+ '&defaultBusisCode=' + dbc 
							//	+  '&busisCodeSet=' + bcs
							//	+  '&sitety=' + sitety
							//	+  '&dateStr=' + dateStr
	;
									
	winUtil.Statics = artDialog.open(url, {
		title : '地方预报',
		width : 700,
		height : 600,
		zIndex : 9999,
		lock : false,
		resize : false,
		opacity : 0.1,
		init : function() {
			_setSelStatus('statics' , true);
			if (callback instanceof Function) {
				callback(true);
			}
		},
		close : function() {
			winUtil.Statics = null;
			_setSelStatus('statics' , false);
		}
	});
};



